<template lang="pug">
  .page
    .AccountsReceivableManage
      .search-form.mb-10.pl-10.pr-10
        Form.ivu-row.ivu-form-basic(ref="formData" :model="formData" :rules="rules" :label-width="70")
          FormItem.form-item.ivu-col.pr-20.mb-15(v-for="(item, index) of formItem" :label="item.label" :key="index" :class="item.style" :prop="item.prop")
            template(v-if="item.type === 'select'")
              Select(v-model="formData[item.model]" :placeholder="item.placeholder" clearable filterable)
                Option(
                  v-for="(c_item, index) in arrListDown[item.options]"
                  :value="c_item.label"
                  :key="index"
                ) {{ c_item.label }}
            template(v-if="item.type === 'input'")
              Input(v-model="formData[item.model]" :placeholder="item.placeholder" clearable)
            template(v-if="item.type === 'button'")
              Button(type="info" @click="searchFn") 查询

      .pl-20.pr-20.bg-white
        Table.beautiful-table(:columns="thead" :data="tableData" :stripe="true" ref="demoTable" @on-selection-change="handleOnSelectChange")
          .clearfix.font-16(slot="header")
            .table-title-lefttxt.pull-left
              |合计
              span.text-info.pl-5.pr-5.font-weight {{tableComone.totalRows}}
              |条
            .pull-right.table-title-btns
              Button.mr-10(type="info" @click="handleShowCreateThirdPartyModal(null)") 新增
              Button.mr-10(type="warning" @click="isDeleteModal" :disabled="!selectedId.length > 0") 删除
          .clearfix.pl-20.pr-20(slot="footer")
            Page(
              :total="tableComone.totalRows"
              @on-change="onChangePage"
              @on-page-size-change="onpageSizeChange"
              show-sizer
              show-total
              placement="top"
              prev-text="上一页"
              next-text="下一页"
              :page-size-opts="[10, 20, 30, 50, 100]"
            )
          template(slot-scope="{ row, index }" slot="action")
            Button(type="text" size="small" @click="handleShowCreateThirdPartyModal(row.id)") 编辑
            Button(type="text" size="small"  v-if="!row.enabled" @click="enabledList(row.id, !row.enabled)") 启用
            Button(type="text" size="small" v-if="row.enabled" @click="enabledList(row.id, !row.enabled)") 禁用
            Button(type="text" size="small" @click="goThirdPartyDetail(row.id)") 详情

    //- 新建弹窗
    CreateThirdPartyModal(ref="CreateThirdPartyModal" @handleSubmit="searchFn")
</template>

<script>
import { Common } from '@/utils/common.js'
import { ThirdPartyApplicationApi } from '@/api/ThirdPartyApplication.api.js'
import { ThirdPartyThead } from './tableHead.js'
import _cloneDeep from 'clone-deep'
import CreateThirdPartyModal from './modal/CreateThirdPartyModal.vue'

export default {
  name: '', // 银行账号管理

  components: {
    CreateThirdPartyModal
  },

  data () {
    return {
      companyId: '', // 公司ID
      companyName: '', // 公司名称
      employeeId: '', // 员工ID
      thead: _cloneDeep(ThirdPartyThead),
      tableData: [],
      selectedId: [], // 勾选的id集合,array格式
      selectedArr: [], // 勾选的id, name数组集合
      tableComone: {
        pageIndex: 1,
        totalRows: 0,
        pageSize: 10
      },
      formData: {
        name: ''
      },
      formItem: [
        { type: 'input', label: '应用名称：', placeholder: '应用名称', prop: 'name', model: 'name', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-9 ivu-col-span-xxl-6', options: 'tempOps' },
        { type: 'button', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-3 ivu-col-span-xxl-3' }
      ],
      rules: {},
      arrListDown: {
        tempOps: []
      },
      importFile: null
    }
  },
  computed: {
  },

  mounted () {
    this.query()
  },

  methods: {
    // 搜索
    searchFn () {
      this.tableComone.pageIndex = 1
      this.query()
    },

    /**
     * 分页页码变动
     * @param {Number} val pageIndex
     */
    onChangePage (val) {
      this.tableComone.pageIndex = val
      this.query()
    },
    /**
     * 分页页码变动
     * @param {Number} val pageSize
     */
    onpageSizeChange (val) {
      this.tableComone.pageIndex = 1
      this.tableComone.pageSize = val
      this.query()
    },
    /**
     * 分页查询接口
     * @param {Object} obj {pageIndex, pageSize}
     */
    async query () {
      var params = {
        HasPaging: true,
        PageIndex: this.tableComone.pageIndex,
        PageSize: this.tableComone.pageSize
      }
      let res = await ThirdPartyApplicationApi.getList({ ...params, ...this.formData })
      console.log('获取列表res: ', res)
      if (res.status === 200 && res.data.success) {
        this.tableData = res.data.data
        this.tableComone.totalRows = res.data.totalRows
      } else {
        this.tableData = []
      }
      this.loading = false
    },

    // 表格选择操作
    handleOnSelectChange (value) {
      this.selectedArr = value.map((item) => {
        return { id: item.id, name: item.invoiceItem }
      })
      this.selectedId = value.map((item) => {
        return item.id
      })
      console.log('列表勾选数组1', this.selectedArr)
      console.log('列表勾选数组2', this.selectedId)
    },

    // 显示新增弹窗
    handleShowCreateThirdPartyModal (id) {
      this.$refs.CreateThirdPartyModal.show()
      this.$refs.CreateThirdPartyModal.createThirdPartyModalTitle = id ? '修改第三方应用信息' : '新增第三方应用信息'
      this.$refs.CreateThirdPartyModal.getModel(id)
    },

    // 启用/禁用
    async enabledList (id, enabled) {
      var params = {
        ids: id,
        enabled: enabled
      }
      let res = await ThirdPartyApplicationApi.batchEnabled(params)
      if (res.status === 200 && res.data.success) {
        this.$Message.success(res.data.message)
        this.query()
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
      this.selectedId = []
      this.selectedIdArr = []
    },

    // 是否删除前询问
    isDeleteModal () {
      this.$Modal.confirm({
        title: '是否确认删除',
        onOk: () => {
          this.handleSureDelete()
        }
      })
    },

    // 确认删除
    async handleSureDelete () {
      let res = await ThirdPartyApplicationApi.delete({ id: this.selectedId.join(',') })
      console.log('确认删除: ', res)
      if (res.status === 200 && res.data.success) {
        this.$Message.success(res.data.message)
        this.query()
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
    },

    // 去往详情
    goThirdPartyDetail (id) {
      this.$router.push({
        path: '/thirdPartyDetail',
        name: 'thirdPartyDetail',
        query: {
          id: id
        }
      })
    }
  } // methods end
}
</script>

<style lang="less" scoped>

</style>
